package com.bilin.coin.network.callback;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import okhttp3.ResponseBody;

/**
 * Created by LG on 2018/4/15.
 */

public abstract class FileCallBack<T extends File> implements Observer<ResponseBody> {

    /**
     * 目标文件存储的文件夹路径
     */
    private String destFileDir;
    /**
     * 目标文件存储的文件名
     */
    private String destFileName;


    public FileCallBack(String destFileDir, String destFileName) {
        this.destFileDir = destFileDir;
        this.destFileName = destFileName;
    }


    @Override
    public void onSubscribe(@NonNull Disposable d) {

    }

    @Override
    public void onNext(@NonNull ResponseBody responseBody) {
        try {
            saveFile(responseBody);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onError(@NonNull Throwable e) {
        onCallBackError(e);
    }

    @Override
    public void onComplete() {

    }

    public abstract void onCallBackNext(File result);

    public abstract void onProgress(float speed, long total);

    public abstract void onCallBackError(Throwable e);


    public void saveFile(final ResponseBody response) throws IOException {
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is = null;
                byte[] buf = new byte[2048];
                int len = 0;
                FileOutputStream fos = null;
                try {
                    is = response.byteStream();
                    final long total = response.contentLength();

                    long sum = 0;

                    File dir = new File(destFileDir);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    File file = new File(dir, destFileName);
                    fos = new FileOutputStream(file);

                    while ((len = is.read(buf)) != -1) {
                        sum += len;
                        fos.write(buf, 0, len);
                        final long finalSum = sum;
                        //LogUtlis.e("----" + finalSum * 1.0f / total + "--------" + total);
                        onProgress(finalSum * 1.0f / total, total);

                    }
                    fos.flush();
                    Thread.sleep(10);
                    onCallBackNext(file);

                } catch (FileNotFoundException e) {


                } catch (IOException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        response.close();
                        if (is != null) is.close();
                    } catch (IOException e) {
                    }
                    try {
                        if (fos != null) fos.close();
                    } catch (IOException e) {
                    }

                }
            }
        }).start();

    }


}
